<?php

class Jaypha_Model extends CI_Model
{

    function __construct()
    {
        parent::__construct();
    }

}

class JayphaDB_Model extends Jaypha_Model
{

    function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取数据表内容
     * @param $offset			偏移量
     * @param $limit			获取指定数量记录
     * @param $fields			获取指定字段
     * @param $tableViewName	待获取的表
     * @param $conditions		获取条件
     * @param $orders			获取记录的排序规则
     * @param $distinct			是否过滤重复项，默认为false不过虑
     * @param $returnType		返回类型，默认为0对象，1为数组
     */
    public function getDB($offset, $limit, $fields, $tableViewName, $conditions, $orders, $distinct, $returnType = 0)
    {
        if (strlen($fields) > 0)
        {
            $this->db->select($fields);
        }

        if (is_array($tableViewName))
        {
            if (array_key_exists('db_join', $tableViewName))
            {
                foreach ($tableViewName['db_join'] as $join_table_arr)
                {
                    if (isset($join_table_arr['joinType']))
                    {
                        $this->db->join($join_table_arr['tableName'], $join_table_arr['conditions'], $join_table_arr['joinType']);
                    } else
                    {
                        $this->db->join($join_table_arr['tableName'], $join_table_arr['conditions']);
                    }
                }
            }
            unset($tableViewName['db_join']);
            $this->db->from($tableViewName['tableName']);
        } elseif (strlen($tableViewName) == 0)
        {
            return;
        } else
        {
            $this->db->from($tableViewName);
        }

        if (is_array($conditions))
        {
            if (array_key_exists('db_where', $conditions))
            {
                $this->db->where($conditions['db_where']);
                unset($conditions['db_where']);
            }
            if (array_key_exists('db_or_where', $conditions))
            {
                $this->db->or_where($conditions['db_or_where']);
                unset($conditions['db_or_where']);
            }
            if (array_key_exists('db_like', $conditions))
            {
                foreach ($conditions['db_like'] as $db_field => $db_value)
                {
                    $this->db->like($db_field, $db_value);
                }
                unset($conditions['db_like']);
            }
            if (array_key_exists('db_not_like', $conditions))
            {
                foreach ($conditions['db_not_like'] as $db_field => $db_value)
                {
                    $this->db->not_like($db_field, $db_value);
                }
                unset($conditions['db_not_like']);
            }
            if (array_key_exists('db_or_like', $conditions))
            {
                foreach ($conditions['db_or_like'] as $db_field => $db_value)
                {
                    $this->db->or_like($db_field, $db_value);
                }
                unset($conditions['db_or_like']);
            }
            if (array_key_exists('db_or_not_like', $conditions))
            {
                foreach ($conditions['db_or_not_like'] as $db_field => $db_value)
                {
                    $this->db->not_or_like($db_field, $db_value);
                }
                unset($conditions['db_or_not_like']);
            }
            if (array_key_exists('db_where_in', $conditions))
            {
                $this->db->where_in($conditions['db_where_in'][0], $conditions['db_where_in'][1]);
                unset($conditions['db_where_in']);
            }
            if (count($conditions) > 0)
            {
                $this->db->where($conditions);
            }
        } elseif (strlen($conditions) > 0)
        {
            $this->db->where($conditions);
        }

        if (strlen($orders) > 0)
        {
            $this->db->order_by($orders);
        }

        if (strlen($offset) > 0 && is_numeric($offset))
        {
            if ($offset > 0)
            {
                if (strlen($limit) && is_numeric($limit))
                {
                    ($limit > 0) ? $this->db->limit($offset, $limit) : $this->db->limit($offset);
                } else
                {
                    $this->db->limit($offset);
                }
            } else
            {
                if (strlen($limit) && is_numeric($limit))
                {
                    if ($limit > 0)
                        $this->db->limit($limit);
                }
            }
        }elseif (strlen($limit) > 0 && is_numeric($limit))
        {
            if ($limit > 0)
                $this->db->limit($limit);
        }

        if (empty($distinct))
        {
            $distinct = false;
        } elseif (!is_bool($distinct))
        {
            $distinct = false;
        }

        if ($distinct)
        {
            $this->db->distinct();
        }
        if ($returnType == 0)
        {
            $returnValue = $this->db->get()->result();
        } else
        {
            $returnValue = $this->db->get()->result_array();
        }
        return $returnValue;
    }
    
    /*
     * 
     */
    public function getDBResult($table, $condition=array(), $fields='*', $order='', $limit=false, $return=1) {
    	$this->db->from($table);
    	if (!empty($condition)) {
    		$this->db->where($condition);
    	}
    	if (!empty($fields)) {
    		$this->db->select($fields);
    	}
    	if (!empty($order)) {
    		$this->db->order_by($order);
    	}
    	if ($limit !== false) {
    		$this->db->limit($limit);
    	}
    	if ($return == 1) {
    		$returnValue = $this->db->get()->result_array();
    	} else {
    		$returnValue = $this->db->get()->result();
    	}
    	return $returnValue;
    }

    /**
     * 获取数据表记录数
     * @param $tableViewName	待获取数据表
     * @param $conditions		获取条件
     */
    public function getDBCount($tableViewName, $conditions)
    {
        //$this->db->cache_on();
        if (is_array($conditions))
        {//判断获取条件是否为数组
            if (array_key_exists('db_where', $conditions))
            {
                $this->db->where($conditions['db_where']);
                unset($conditions['db_where']);
            }
            if (array_key_exists('db_or_where', $conditions))
            {
                $this->db->or_where($conditions['db_or_where']);
                unset($conditions['db_or_where']);
            }
            if (array_key_exists('db_like', $conditions))
            {
                foreach ($conditions['db_like'] as $db_field => $db_value)
                {
                    $this->db->like($db_field, $db_value);
                }
                unset($conditions['db_like']);
            }
            if (array_key_exists('db_not_like', $conditions))
            {
                foreach ($conditions['db_not_like'] as $db_field => $db_value)
                {
                    $this->db->not_like($db_field, $db_value);
                }
                unset($conditions['db_not_like']);
            }
            if (array_key_exists('db_or_like', $conditions))
            {
                foreach ($conditions['db_or_like'] as $db_field => $db_value)
                {
                    $this->db->or_like($db_field, $db_value);
                }
                unset($conditions['db_or_like']);
            }
            if (array_key_exists('db_or_not_like', $conditions))
            {
                foreach ($conditions['db_or_not_like'] as $db_field => $db_value)
                {
                    $this->db->not_or_like($db_field, $db_value);
                }
                unset($conditions['db_or_not_like']);
            }
            if (array_key_exists('db_where_in', $conditions))
            {
                $this->db->where_in($conditions['db_where_in'][0], $conditions['db_where_in'][1]);
                unset($conditions['db_where_in']);
            }
            if (count($conditions) > 0)
            {
                $this->db->where($conditions);
            }
        } elseif (strlen($conditions) > 0)
        {
            $this->db->where($conditions);
        }

        if (is_array($tableViewName))
        {
            if (array_key_exists('db_join', $tableViewName))
            {
                foreach ($tableViewName['db_join'] as $join_table_arr)
                {
                    if (isset($join_table_arr['joinType']))
                    {
                        $this->db->join($join_table_arr['tableName'], $join_table_arr['conditions'], $join_table_arr['joinType']);
                    } else
                    {
                        $this->db->join($join_table_arr['tableName'], $join_table_arr['conditions']);
                    }
                }
            }
            unset($tableViewName['db_join']);
            $this->db->from($tableViewName['tableName']);
        } elseif (strlen($tableViewName) == 0)
        {
            return;
        } else
        {
            $this->db->from($tableViewName);
        }
        return $this->db->count_all_results();
        //$this->db->cache_off();
    }

    /**
     * 获取字段值
     * @param $field			获取字段
     * @param $tableViewName	获取字段所在表
     * @param $conditions		获取字段条件
     * @param $order			排序方式
     */
    public function getDBValue($field, $tableViewName, $conditions, $orders)
    {
        //$this->db->cache_on();
        $fieldsList = '';
        if (is_array($field))
        {
            if (array_key_exists('db_fields_list', $field))
            {
                $fieldsList = $field['db_fields_list'];
            }
            unset($field['db_fields_list']);
            $field = $field['field'];
        } else
        {
            if (strlen($field) > 0)
                $fieldsList = $field;
        }
        foreach ($this->getDB(0, 1, $fieldsList, $tableViewName, $conditions, $orders, false, 0) as $item)
        {
            return $item->$field;
        }
    }

    /**
     * 插入数据
     * @param $tableViewName	待插入数据表
     * @param $dataArray		字段及值（以数组进行传递）
     * @param $escape			数据是否转义（取值范围：true<对数据进行转义>|false<不对数据进行转义>默认为true）
     */
    public function insertDB($tableViewName, $dataArray, $escape)
    {
        if (strlen($escape) == 0)
        {
            $escape = true;
        } elseif (!is_bool($escape))
        {
            $escape = true;
        }
        if ($escape)
        {
            $this->db->set($dataArray);
        } else
        {
            $this->db->set($dataArray, $escape);
        }
        return $this->db->insert($tableViewName);
    }

    /**
     * 更新数据
     * @param $valuesData		数据组
     * @param $tableViewName	待更新的数据表
     * @param $conditions		更新条件
     */
    public function updateDB($valuesData, $tableViewName, $conditions)
    {
        if (is_array($conditions))
        {
            if (array_key_exists('db_where', $conditions))
            {
                $this->db->where($conditions['db_where']);
                unset($conditions['db_where']);
            }
            if (array_key_exists('db_or_where', $conditions))
            {
                $this->db->or_where($conditions['db_or_where']);
                unset($conditions['db_or_where']);
            }
            if (array_key_exists('db_like', $conditions))
            {
                foreach ($conditions['db_like'] as $db_field => $db_value)
                {
                    $this->db->like($db_field, $db_value);
                }
                unset($conditions['db_like']);
            }
            if (array_key_exists('db_not_like', $conditions))
            {
                foreach ($conditions['db_not_like'] as $db_field => $db_value)
                {
                    $this->db->not_like($db_field, $db_value);
                }
                unset($conditions['db_not_like']);
            }
            if (array_key_exists('db_or_like', $conditions))
            {
                foreach ($conditions['db_or_like'] as $db_field => $db_value)
                {
                    $this->db->or_like($db_field, $db_value);
                }
                unset($conditions['db_or_like']);
            }
            if (array_key_exists('db_or_not_like', $conditions))
            {
                foreach ($conditions['db_or_not_like'] as $db_field => $db_value)
                {
                    $this->db->not_or_like($db_field, $db_value);
                }
                unset($conditions['db_or_not_like']);
            }
            if (array_key_exists('db_where_in', $conditions))
            {
                $this->db->where_in($conditions['db_where_in'][0], $conditions['db_where_in'][1]);
                unset($conditions['db_where_in']);
            }
            if (count($conditions) > 0)
            {
                $this->db->where($conditions);
            }
        } elseif (strlen($conditions) > 0)
        {
            $this->db->where($conditions);
        }
        return $this->db->update($tableViewName, $valuesData);
    }

    /**
     * 删除数据表中的数据
     * @param $tableViewName	待删除数据的表
     * @param $conditions		删除条件
     */
    public function delDB($tableViewName, $conditions)
    {
        if (is_array($conditions) > 0)
        {
            if (array_key_exists('db_where', $conditions))
            {
                $this->db->where($conditions['db_where']);
                unset($conditions['db_where']);
            }
            if (array_key_exists('db_or_where', $conditions))
            {
                $this->db->or_where($conditions['db_or_where']);
                unset($conditions['db_or_where']);
            }
            if (array_key_exists('db_like', $conditions))
            {
                foreach ($conditions['db_like'] as $db_field => $db_value)
                {
                    $this->db->like($db_field, $db_value);
                }
                unset($conditions['db_like']);
            }
            if (array_key_exists('db_not_like', $conditions))
            {
                foreach ($conditions['db_not_like'] as $db_field => $db_value)
                {
                    $this->db->not_like($db_field, $db_value);
                }
                unset($conditions['db_not_like']);
            }
            if (array_key_exists('db_or_like', $conditions))
            {
                foreach ($conditions['db_or_like'] as $db_field => $db_value)
                {
                    $this->db->or_like($db_field, $db_value);
                }
                unset($conditions['db_or_like']);
            }
            if (array_key_exists('db_or_not_like', $conditions))
            {
                foreach ($conditions['db_or_not_like'] as $db_field => $db_value)
                {
                    $this->db->not_or_like($db_field, $db_value);
                }
                unset($conditions['db_or_not_like']);
            }
            if (array_key_exists('db_where_in', $conditions))
            {
                $this->db->where_in($conditions['db_where_in'][0], $conditions['db_where_in'][1]);
                unset($conditions['db_where_in']);
            }
            if (count($conditions) > 0)
            {
                $this->db->where($conditions);
            }
        } elseif (strlen($conditions) > 0)
        {
            $this->db->where($conditions);
        }

        if (empty($tableViewName) || strlen($tableViewName) == 0)
        {
            return;
        } else
        {
            if (!is_array($conditions))
            {
                if (strlen($conditions) == 0)
                    return $this->db->empty_table($tableViewName);
                else
                    return $this->db->delete($tableViewName);
            }else
            {
                if (count($conditions) > 0)
                    return $this->db->delete($tableViewName);
                else
                    return $this->db->empty_table($tableViewName);
            }
        }
    }

    /**
     * 匹配表单上传数据
     * @param array $data 
     * @return Array
     */
    public function create($data,$tableViewName)
    {
        $list = array();
        if (!empty($data) && is_array($data))
        {   //如果存在$_POST数据 则进行数据筛选
            foreach ($this->db->list_fields("t_".$tableViewName) as $field)
            {
                foreach ($data as $key => $val)
                {
                    if ($field == $key)
                    {   //取出与数据库字段名相同的POST值
                        if (!get_magic_quotes_gpc())
                            $list[$key] = addslashes(strip_tags($val));
                        else
                            $list[$key] = strip_tags($val);
                    }
                }
            }
            return $list;
        }else
        {
            return false;
        }
    }

}